package supermarket.resources;


import java.util.List;

import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;

import supermarket.dao.Shop;
import supermarket.model.Product;
import supermarket.model.ProductList;

// The Java class will be hosted at the URI path "/products"
@Path("/products")
public class ProductsResource {
		
	private Shop shopService;
	
	public void setShopService(Shop shopService) {
		this.shopService = shopService;
	}
	
	// The Java method will process HTTP POST requests
    @GET
    @Produces({"application/xml", "application/json"})
    public ProductList getProducts(@QueryParam("keyword") String keyword, @QueryParam("search") String search) {
    	ProductList productList = new ProductList();
    	List catalog = null;
    	if(search.equalsIgnoreCase("name"))
    		catalog = (List)shopService.getProductByName(keyword);
    	else
    		catalog = (List)shopService.getProductByCategory(keyword);
    	productList.setCatalog(catalog);
    	return productList;
    }
    
    // The Java method will process HTTP GET requests
    @GET @Path("/list")
    @Produces(MediaType.APPLICATION_XML)
    public ProductList getProductList() {
    	ProductList productList = new ProductList();
    	List catalog = (List)shopService.getProductList();
    	productList.setCatalog(catalog);
    	return productList;
    }
    
    // The Java method will process HTTP GET requests
    @GET @Path("/{id}")
    @Produces(MediaType.APPLICATION_JSON)
    public Product getProductList(@PathParam("id") Integer id) {
    	return (Product)shopService.getProduct(id);
    }
    
    @PUT @Path("/add/{name}")
    public void addProduct(@PathParam("name") String name) {
    	shopService.addProduct(new Product(name));
    }
    
    @DELETE @Path("/remove/{id}")
    public void removeProduct(@PathParam("id") Integer id) {
    	shopService.removeCustomer(shopService.getCustomer(id));
    }
}